import Common from '../../../common/common.js';
let common = new Common;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    //到账日期
    thawDate: '',
    //2待确认3已确认
    status: 2,
    //对账总体情况
    summary: {},
    //订单列表
    list: [],
    //是否有数据
    hasData: true,
    //是否显示底部按钮和复选框
    manageOpe: false,
    //是否全选
    checkedAll: false,
  },
  /**
   *  获取单日对账单统计信息
   */
  getSummary: function () {
    var that = this;
    var data = {
      "orgSeq": getApp().globalData.orgSeq,
      "thawDate": that.data.thawDate,
      "status": that.data.status
    };
    wx.showLoading({
      title: '加载中',
      mask: true
    })
    common.requestAjax('/api/pay-service/v1/payTrade/queryDayPayRecordInfo', 'POST', data).then(res => {
      wx.hideLoading();
      if (res.data.code == 1) {
        var summary = res.data.result;
        let list = summary.orderList;
        //金额为两位小数
        summary.receivedMoney = summary.receivedMoney.toFixed(2);
        summary.totalMoney = summary.totalMoney.toFixed(2);
        summary.discountMoney = summary.discountMoney.toFixed(2);
        for (var i = 0; i < list.length; i++) {
          list[i].receivedMoney = list[i].receivedMoney.toFixed(2);
          list[i].totalMoney = list[i].totalMoney.toFixed(2);
          list[i].discountMoney = list[i].discountMoney.toFixed(2);
        }
        this.setData({
          summary: summary,
          list: list
        })
        if (list == null || list.length == 0) {
          that.setData({
            hasData: false
          })
        }
      } else {
        common.showToast(res.data.message);
      }
    }).catch(err => {
      common.showToast(err);
    })
  },
  /**
   * 开启对账管理
   */
  setManage: function () {
    var list = this.data.list;
    if (list.length == 0) {
      common.showToast("没有需要对账的记录");
      return;
    }
    this.setData({
      manageOpe: true
    })
  },
  /**
   * 确认对账
   */
  saveManage: function () {
    var that = this;
    var data = that.getChoosedItem();
    if (data.length == 0) {
      common.showToast("清选择一条记录");
      return;
    }
    var content = '已选择'+data.length+'条记录，确认对账吗？';
    wx.showModal({
      title: '温馨提示',
      content: content,
      confirmText: "确认",
      cancelText: "取消",
      success: function (res) {
        if (res.confirm) {
          wx.showLoading({
            title: '加载中',
            mask: true
          })
          var dataWrap = {
            "orgSeq": getApp().globalData.orgSeq,
            "confirmList": data
          }
          common.requestAjax('/api/pay-service/v1/payTrade/confirmPayRecords', 'POST', dataWrap).then(res => {
            wx.hideLoading();
            if (res.data.code == 1) {
              common.showToast(res.data.message);
              that.getSummary();
            } else {
              common.showToast(res.data.message);
            }
          }).catch(err => {
            wx.hideLoading();
            common.showToast("系统异常，请稍后重试！");
          })
        }
      }
    });
  },
  /**
   * 取消权限
   */
  cancleManage: function () {
    this.setData({
      manageOpe: false
    })
  },
  /**
   * 选择记录
   */
  checkboxChange: function (e) {
    var uuid = e.currentTarget.dataset.uuid;
    var valueArray = e.detail.value;
    var checked = false;
    if (valueArray[0] == uuid) {
      //如果uuid值和checkbox返回的值相等，表示选中
      checked = true;
    }
    var list = this.data.list;
    for (var i = 0; i < list.length; i++) {
      if (list[i].uuid == uuid) {
        list[i].checked = checked;
        break;
      }
    }
    this.setData({
      list: list
    })
  },
  /**
   * 全选
   */
  checkboxChangeAll: function (e) {
    var checked = e.currentTarget.dataset.checkedall;
    checked = !Boolean(checked);
    var list = this.data.list;
    for (var i = 0; i < list.length; i++) {
      list[i].checked = checked;
    }
    this.setData({
      list: list,
      checkedAll: checked
    })
  },
  /**
   * 获取已选择数据列表
   */
  getChoosedItem: function () {
    var that = this;
    var data = [];
    var list = that.data.list;
    for (var j = 0; j < list.length; j++) {
      var checked = list[j].checked;
      if (checked) {
        data.push(
          list[j].uuid
        )
      }
    }
    return data;
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let item = options.item;
    if (item != null) {
      item = JSON.parse(item);
    }
    this.setData({
      thawDate: item.thawDate,
      status: item.status
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    // getApp().globalData.orgSeq = "2e8f4c46-ee3b-4b4b-9270-cf00c28271af";
    // this.setData({
    //   thawDate: '2020-04-23',
    //   status: 2
    // })
    //获取数据
    this.getSummary();
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})